package com.example.kafkajavaopetion;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @ClassName kafkaProducerOps
 * @Description TODO
 * @Author zhangxidong
 * @Date 2020/11/24
 * @Version 1.0
 */
public class kafkaProducerOps {

	public static void main(String[] args) throws IOException {
		Properties properties = new Properties();
		InputStream in = kafkaProducerOps.class.getClassLoader().getResourceAsStream("producer.properties");
		properties.load(in);

		String[] girls = new String[]{"小红", "小王", "小张", "小李"};
		Producer<String, String> producer = new KafkaProducer<String, String>(properties);
		String topic = properties.getProperty(Constants.KAFKA_PRODUCER_TOPIC);
		for (int i = 0; i < girls.length; i++) {
			String key = i + "";
			String value = girls[i];
			ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(topic, key, value);
			producer.send(producerRecord);
		}
		producer.close();
	}
}
